# coding=utf-8

import datetime
import urllib2

# 获取访问网址所需要的时间，包括平均时间
def process(url, n, m):
    minSpan = 10.0
    maxSpan = 0.0
    sumSpan = 0.0
    overXs = 0
    start = datetime.datetime.now()
    for i in range(n):
        startTime = datetime.datetime.now()
        try:
            res = urllib2.urlopen(url, timeout=10)
        except:
            pass
        endTime = datetime.datetime.now()
        span = (endTime - startTime).total_seconds()
        sumSpan = sumSpan + span
        if span < minSpan:
            minSpan = span
        if span > maxSpan:
            maxSpan = span
        # 超过m秒的次数
        if span > m:
            overXs = overXs + 1
    # end = datetime.datetime.now()
    # print url
    # print (u'请求次数：%s 次；总的时间：%s 秒；平均时间：%s 秒；最大时间：%s 秒；最小时间：'
    #        u'%s 秒；超过%s秒的次数：%s 次。' %(n, sumSpan, (sumSpan/n), maxSpan, minSpan, m, overXs))
    if overXs != 0:
        timeError = u"总共%s次，超过%s秒次数：%s次！" %(n, m, overXs)
        return timeError
    else:
        pass

# if __name__ == '__main__':
#     print process("http://www.baidu.com", 10, 1)
#     print process("http://m.cbi360.net/hhb", 3, 1)

